// 获取全局应用实例
const app = getApp();

// 定义默认主题配置
const defaultThemes = {
  white: {
    primary: '#FFFFFF',
    secondary: '#F5F5F5',
    accent: '#4A90E2',
    text: '#333333',
    nav: '#2C6DB4'
  },
  green: {
    primary: '#F0FFF3',
    secondary: '#C8E6C9',
    accent: '#81C784',
    text: '#2C4A3E',
    nav: '#4CAF50'
  },
  purple: {
    primary: '#F5F3FF',
    secondary: '#E8E2FF',
    accent: '#B39DDB',
    text: '#4A4453',
    nav: '#7E57C2'
  },
  blue: {
    primary: '#F0F9FF',
    secondary: '#BBDEFB',
    accent: '#64B5F6',
    text: '#2C3E50',
    nav: '#1E88E5'
  }
};

Page({
  data: {
    theme: defaultThemes.purple, // 默认主题
    userInfo: {},
    studyData: {
      days: 0,
      hours: 0,
      streak: 0
    },
    settings: {
      notification: true
    },
    themes: [
      { name: '纯净白', key: 'white' },
      { name: '护眼绿', key: 'green' },
      { name: '浅紫色', key: 'purple' },
      { name: '浅蓝色', key: 'blue' }
    ],
    showThemeDialog: false
  },

  onLoad() {
    this.loadLocalData();
    this.syncGlobalTheme(); // 同步全局主题
  },

  // 加载本地数据
  loadLocalData() {
    const themeKey = wx.getStorageSync('theme') || 'purple';
    this.setData({
      theme: defaultThemes[themeKey],
      userInfo: wx.getStorageSync('userInfo') || {},
      studyData: wx.getStorageSync('studyData') || this.data.studyData,
      settings: {
        notification: wx.getStorageSync('notification') ?? true
      }
    });
  },

  // 同步全局主题
  syncGlobalTheme() {
    if (app.globalData.theme) {
      this.setData({ theme: app.globalData.theme });
    }
  },

  // 选择头像
  chooseAvatar() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: res => {
        const newAvatar = res.tempFiles[0].tempFilePath;
        this.setData({
          'userInfo.avatar': newAvatar
        });
        wx.setStorageSync('userInfo', this.data.userInfo);
      }
    });
  },

  // 修改昵称
  editNickname() {
    wx.showModal({
      title: '修改昵称',
      content: '请输入新昵称',
      editable: true,
      success: res => {
        if (res.confirm && res.content) {
          this.setData({
            'userInfo.nickname': res.content
          });
          wx.setStorageSync('userInfo', this.data.userInfo);
        }
      }
    });
  },

  // 修改签名
  editMotto() {
    wx.showModal({
      title: '个性签名',
      content: '请输入个性签名',
      editable: true,
      success: res => {
        if (res.confirm) {
          this.setData({
            'userInfo.motto': res.content
          });
          wx.setStorageSync('userInfo', this.data.userInfo);
        }
      }
    });
  },

  // 切换主题
  toggleTheme() {
    this.setData({ showThemeDialog: true });
  },

  // 选择主题
  selectTheme(e) {
    const key = e.currentTarget.dataset.key;
    const newTheme = defaultThemes[key];
    app.updateTheme(newTheme);
    wx.setStorageSync('theme', key);
    this.setData({ theme: newTheme, showThemeDialog: false });
  },

  // 隐藏主题弹窗
  hideThemeDialog() {
    this.setData({ showThemeDialog: false });
  },

  // 切换通知
  toggleNotification() {
    const newValue = !this.data.settings.notification;
    this.setData({
      'settings.notification': newValue
    });
    wx.setStorageSync('notification', newValue);
  },

  // 显示帮助
  showHelp() {
    wx.showModal({
      title: '帮助中心',
      content: '请联系客服：400-123-4567',
      showCancel: false
    });
  },

  // 编辑介绍
  editIntroduction() {
    wx.showModal({
      title: '个人介绍',
      content: '请输入个人介绍',
      editable: true,
      success: res => {
        if (res.confirm) {
          this.setData({
            'userInfo.intro': res.content
          });
          wx.setStorageSync('userInfo', this.data.userInfo);
        }
      }
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: res => {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          this.setData({ userInfo: {} });
          wx.reLaunch({ url: '/pages/index/index' });
        }
      }
    });
  }
});